/**
 * jims
 */
package com.jims.master.modules.finance.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.entity.PatVisitIndex;
import com.jims.master.modules.doctor.service.PatVisitIndexService;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.finance.entity.InpSettleMaster;
import com.jims.master.modules.finance.service.InpBillDetailService;
import com.jims.master.modules.finance.service.InpSettleMasterService;
import com.jims.master.modules.finance.service.MedicalInsuranceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @version 2017-03-30
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/medicalInsurance")
public class medicalInsuranceController extends BaseController {
    @Autowired
    private MedicalInsuranceService medicalInsuranceService;

    @Autowired
    private InpSettleMasterService inpSettleMasterService;

    @Autowired
    private PatVisitIndexService patVisitIndexService;

    @Autowired
    private InpBillDetailService inpBillDetailService;

    /**
     * 医保住院数据传输查询
     *
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index"})
    public String list(InpSettleMaster inpSettleMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<InpSettleMaster> page = new Page<InpSettleMaster>();
        page = inpSettleMasterService.findPageMedicalInsurance(new Page<InpSettleMaster>(request, response), inpSettleMaster);
        model.addAttribute("page", page);
        return "modules/med/medicalInsurIndex";
    }

    /**
     * 住院医保--住院/出院病人
     * 查询医保住院登记记录
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"patIndex"})
    public String patIndex(PatVisitIndex patVisitIndex,HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<PatVisitIndex> page = new Page<PatVisitIndex>();
        page= patVisitIndexService.getMedInsInpPatList(new Page<PatVisitIndex>(request, response), patVisitIndex);
        model.addAttribute("page", page);
        return "modules/med/medInsTranInpPatIndex";
    }
    /**
     * 住院医保--病人费用详情信息
     * 查询医保登记病人费用全部信息
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"patInpBillDetail"})
    public String patInpBillDetail(InpBillDetail inpBillDetail,HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<InpBillDetail> page = new Page<InpBillDetail>();
        page = inpBillDetailService.getMedInsInpBillDetail(new Page<InpBillDetail>(request, response),inpBillDetail);
        model.addAttribute("page", page);
        return "modules/med/medInsTranInpIndex";
    }
    /**
     * 医保住院-查询是否医保中心登记
     *
     * @param visitNo
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findSickVisitInfo"})
    @ResponseBody
    public StringData findSickVisitInfo(String visitNo) {
        StringData stringData = new StringData();
        String recordMaster = medicalInsuranceService.findSickVisitInfo(visitNo);
        if ("success".equals(recordMaster)) {
            stringData.setCode("success");
            stringData.setData("该病人已在医保登记住院信息");
        } else {
            stringData.setCode("error");
            stringData.setData("该病人未在医保登记住院信息,请先登记后再报销");
        }
        return stringData;
    }

    /**
     * 医保结算主信息-调回门诊医保结算信息
     *
     * @param inParams （visitNumber）
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findRecordMaster"})
    @ResponseBody
    public StringData findRecordMaster(String inParams, String pay_type) {
        StringData stringData = new StringData();
        String recordMaster = medicalInsuranceService.findRecordMaster(inParams, pay_type);
        if (StringUtils.isNotBlank(recordMaster)) {
            stringData.setCode("success");
            stringData.setData(recordMaster);
        } else {
            stringData.setCode("error");
            stringData.setData(null);
        }
        return stringData;
    }

    /**
     * 医保结算主信息-调回住院医保结算信息
     *
     * @param visitNo
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findRecordMasterInp"})
    @ResponseBody
    public StringData findRecordMasterInp(String visitNo) {
        StringData stringData = new StringData();
        String recordMaster = medicalInsuranceService.findRecordMasterInp(visitNo);
        if (StringUtils.isNotBlank(recordMaster)) {
            stringData.setCode("success");
            stringData.setData(recordMaster);
        } else {
            stringData.setCode("error");
            stringData.setData(null);
        }
        return stringData;
    }

    /**
     * 住院医保撤回
     *
     * @param visitNo
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"medicalInsurRefund"})
    @ResponseBody
    public StringData medicalInsurRefund(String visitNo) {
        StringData stringData = new StringData();
        String medicalInsurRefund = medicalInsuranceService.medicalInsurRefund(visitNo);
        if ("success".equals(medicalInsurRefund)) {
            stringData.setCode("success");
            stringData.setData("撤回成功");
        } else {
            stringData.setCode("error");
            stringData.setData("撤回失败,请记录，并联系相关人员处理");
        }
        return stringData;
    }

    /**
     * 医保报销-住院数据上传
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveKC27")
    @ResponseBody
    public StringData saveKC27(String visitNo) {
        StringData stringData = new StringData();
        String num = medicalInsuranceService.saveKC27(visitNo);
        if ("success".equals(num)) {
            stringData.setCode("success");
            stringData.setData("住院数据上传医保成功");
        } else {
            stringData.setCode("error");
            stringData.setData("住院数据上传医保失败");
        }
        return stringData;
    }

    /**
     * 医保报销-门诊处方数据上传
     *
     * @param clinicNo
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(String functionName, String inParams, String clinicNo) {
        StringData stringData = new StringData();
        String serialNo = medicalInsuranceService.saveYibaoServer(inParams, clinicNo);
        if (StringUtils.isNotBlank(serialNo)) {
            stringData.setCode("success");
            stringData.setData(serialNo);
        } else {
            stringData.setCode("error");
            stringData.setData("上传失败");
        }
        return stringData;
    }

    /**
     * test-结算主记录防重复插入
     * @param personalNumber
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "testInsertSelect")
    @ResponseBody
    public StringData testInsertSelect(String personalNumber) {
        StringData stringData = new StringData();
        personalNumber="130828198708160831";
        String serialNo = medicalInsuranceService.testInsertSelect(personalNumber);
        if (StringUtils.isNotBlank(serialNo)) {
            stringData.setCode("success");
            stringData.setData(serialNo);
        } else {
            stringData.setCode("error");
            stringData.setData("上传失败");
        }
        return stringData;
    }
}